import Dep from "./dep.js";
export default class Watcher {
  constructor(vm, exp, cb) {
    this.vm = vm;
    this.exp = exp;
    this.cb = cb;
    this.value = this.get();
  }
  get() {
    Dep.target = this;
    const value = this.vm[this.exp]; // new Watcher时强制添加
    Dep.target = null;
    return value;
  }
  update() {
    this.run();
  }
  run() {
    let value = this.vm[this.exp];
    let oldVal = this.value;
    if (oldVal === value) return;
    this.value = value;
    this.cb && this.cb.call(this.vm, value, oldVal);
  }
}
